{
  "cells": [
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Getting started with Integrated Hybrid Computing\n",
        "In this sample, you will discover how to blend classical and quantum instructions in the same program, all fully processed by the quantum computing backend.\n",
        "You can learn more about integrated hybrid computing at https://aka.ms/AQ/Hybrid/Docs\n",
        "\n",
        "## Connect to the Azure Quantum workspace\n",
        "First, find the resource ID of your Azure Quantum workspace. You can copy/paste this from the top-right corner of your Quantum Workspace page in the Azure Portal.\n",
        "\n",
        "Next, you can run `%azure.connect` to connect to the workspace and display the list of provisioned targets that support execution of Q# programs. If you are prompted to login, be sure to use the same account you used to create your Azure Quantum workspace."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "jupyter": {
          "outputs_hidden": false,
          "source_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      },
      "outputs": [],
      "source": [
        "%azure.connect \"/subscriptions/subscriptionId/resourceGroups/resourceGroupName/providers/Microsoft.Quantum/Workspaces/workspaceName\" location=\"westus\""
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Select a target and opt-in to use integrated hybrid computing\n",
        "First, we select the target the program is intended to be run on using the `%azure.target` command. This information is used by Q# compiler to check the program can be run on the specified target taking into consideration its capabilities.\n",
        "\n",
        "Since integrated hybrid computing is an opt-in feature, we run the `%azure.target-capability` command to opt into it.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "%azure.target quantinuum.sim.h1-1e\n",
        "%azure.target-capability AdaptiveExecution"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      },
      "source": [
        "## Define the CheckGHZ operation\n",
        "\n",
        "This program prepares a Greenberger–Horne–Zeilinger (GHZ) state and counts the number of times qubit measurements are not perfectly correlated out of 10 attempts. Unlike in noiseless simulations, non-perfect correlations can occur when programs run on real quantum devices which are subject to the effects of noise.\n",
        "\n",
        "In the CheckGHZ() operation, we can see two capabilities of integrated hybrid computing being demonstrated:\n",
        "1. **Branching** based on qubit measurements, which allows programs to execute different quantum operations or classical instructions depending on the results yielded by qubit measurements.\n",
        "```Q#\n",
        "   if not (r0 == r1 and r1 == r2)\n",
        "```\n",
        "2. **Classical compute** performed in real time by the quantum machine itself.\n",
        "```Q#\n",
        "   set mismatch += 1;\n",
        "```\n",
        "### ❕ Please note that you will see a warning displayed (QS5028)\n",
        "It can be ignored for the purposes of this sample. It will be addressed in a subsequent release of the Quantum Development Kit."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "jupyter": {
          "outputs_hidden": false,
          "source_hidden": false
        },
        "microsoft": {},
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      },
      "outputs": [],
      "source": [
        "open Microsoft.Quantum.Intrinsic;\n",
        "open Microsoft.Quantum.Measurement;\n",
        "\n",
        "operation CheckGHZ() : Int {\n",
        "    use q = Qubit[3];\n",
        "    mutable mismatch = 0;\n",
        "    for _ in 1..10 {\n",
        "        // Prepare the GHZ state.\n",
        "        H(q[0]);\n",
        "        CNOT(q[0], q[1]);\n",
        "        CNOT(q[1], q[2]);\n",
        "\n",
        "        // Measures and resets the 3 qubits\n",
        "        let (r0, r1, r2) = (MResetZ(q[0]), MResetZ(q[1]), MResetZ(q[2]));\n",
        "\n",
        "        // Adjusts classical value based on qubit measurement results\n",
        "        if not (r0 == r1 and r1 == r2) {\n",
        "            set mismatch += 1;\n",
        "        }\n",
        "    }\n",
        "    return mismatch;\n",
        "}"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Submit the program\n",
        "### ❕ This program will use approximately 10.65 units (HQCs or eHQCs) using 50 shots, as configured below.\n",
        "If the job has not been completed after the client times out (300 seconds), you can query the status using `%azure.status` followed by querying the output using `%azure.output`.<br/>\n",
        "\n",
        "If the kernel was restarted after the program had been submitted, you will have to use `%azure.status <job_id>` to get the status of the job and `%azure.output <job_id>` to retrieve the results.<br/>\n",
        "You can also check the status of the job under the **Job management** section on the left side menu of the portal.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "%azure.execute CheckGHZ shots=50 timeout=300 poll=10"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "%azure.status"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Understanding the results\n",
        "Each item in the histogram represents the correlation mismatch count and the corresponding frequency the mismatch count was observed out of the number of shots that were run.<br/>\n",
        "The histogram values can range from 0 (no correlation mismatch occurred) to 10 (all correlations checks were a mismatch)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "%azure.output"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      },
      "source": [
        "## ↗ Learning more about Integrated Hybrid Computing\n",
        "Now that you have run this sample, you can further learn and experiment with other samples available in the gallery.\n",
        "\n",
        "To learn more, please refer to our [online documentation](https://aka.ms/AQ/Hybrid/Docs)."
      ]
    }
  ],
  "metadata": {
    "kernel_info": {
      "name": "iqsharp"
    },
    "kernelspec": {
      "display_name": "Q#",
      "language": "qsharp",
      "name": "iqsharp"
    },
    "language_info": {
      "file_extension": ".qs",
      "mimetype": "text/x-qsharp",
      "name": "qsharp",
      "version": "0.27"
    },
    "nteract": {
      "version": "nteract-front-end@1.0.0"
    },
    "vscode": {
      "interpreter": {
        "hash": "5928ddfb481d60843a3b943727ce04da7552e01006ad87377dd54780c5247bec"
      }
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
